package com.jueye.onlinetalk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jueye.onlinetalk.entry.User;
import com.jueye.onlinetalk.entry.bo.UserBo;
import com.jueye.onlinetalk.entry.vo.RoleVo;
import com.jueye.onlinetalk.entry.vo.UserVo;
import com.jueye.onlinetalk.mapper.RoleMapper;
import com.jueye.onlinetalk.service.UserRoleRelService;
import com.jueye.onlinetalk.service.UserService;
import com.jueye.onlinetalk.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author ASUS
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2025-07-07 11:37:47
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleRelService userRoleRelService;



    public QueryWrapper<User> getQueryWrapper(UserBo userBo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (userBo != null) {
            queryWrapper.eq(userBo.getId() != null, "id", userBo.getId());
            queryWrapper.like(userBo.getUsername() != null, "name", userBo.getUsername());
            queryWrapper.eq(userBo.getUseraccount() != null, "useraccount", userBo.getUseraccount());
        }
        return queryWrapper;
    }

    @Override
    public UserVo findByUserBo(UserBo userBo) {
        User user = userMapper.selectOne(getQueryWrapper(userBo));
        return this.getUserVo(user);
    }

    private UserVo getUserVo(User user) {
        UserVo userVo = new UserVo();
        List<RoleVo> roleVos = userRoleRelService.getRoleVosByUserId(user.getId());
        BeanUtils.copyProperties(user, userVo);
        userVo.setRoles(roleVos);
        return userVo;
    }
}




